//
//  ViewController.m
//  RandomAccount
//
//  Created by cuibo on 2020/9/15.
//  Copyright © 2020 cuibo. All rights reserved.
//
#define RGB(r, g, b)             [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define RGBAlpha(r, g, b, a)     [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

#define  IS_iPhoneX (CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size)  || CGSizeEqualToSize(CGSizeMake(414.f, 896.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(896.f, 414.f), [UIScreen mainScreen].bounds.size))
 #define SafeAreaTopHeight (IS_iPhoneX ? 88 : 64)
/// 底部宏，
#define SafeAreaBottomHeight (IS_iPhoneX ? 34 : 0)
#define SafeAreaHomeBottomHeight (IS_iPhoneX ? 83 : 49)


#import "ViewController.h"
#import "TextFieldView.h"
#import "SwitchMiddleView.h"
typedef NS_ENUM(NSUInteger, Type) {
    TypeSingle,
    TypeMany,
};
@interface ViewController ()
{
    CAGradientLayer * _graLayer;
    TextFieldView * _minField;
    TextFieldView * _maxField;
    TextFieldView * _countField;
    SwitchMiddleView * _segmenView;
    UILabel * _contenlab;
    UILabel * _tipsLb;
    NSInteger _count;
    NSInteger _lastMin;
    NSInteger _lastMax;
    NSInteger _lastCount;
    BOOL _repeat;
};
@property (nonatomic, assign) Type type;
@property (nonatomic, strong) NSMutableArray *numbers;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    [self setGraColors];
    [self touchesBegan:nil withEvent:nil];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
       [self becomeFirstResponder];
    self.type = TypeSingle;
    _repeat = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        [self caculRandomNumer];
    }
}
- (void)addSubViews {
    _graLayer = [CAGradientLayer layer];
    _graLayer.frame = self.view.frame;
    [self.view.layer addSublayer:_graLayer];
    NSArray * titles = @[@"最小数",@"最大数",@"生成数量"];
    CGFloat w = self.view.frame.size.width / 3;
    CGFloat h = 50;
    _minField = [[TextFieldView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, w, h)];
    [_minField setTitle:titles[0]];
    [_minField setFieldNumber:1];
    _minField.block = ^(NSString * _Nullable content) {
        [self.numbers removeAllObjects];
    };
    _maxField = [[TextFieldView alloc] initWithFrame:CGRectMake(w, SafeAreaTopHeight, w, h)];
    [_maxField setTitle:titles[1]];
    [_maxField setFieldNumber:100];
    _maxField.block = ^(NSString * _Nullable content) {
        [self.numbers removeAllObjects];
    };
    _countField = [[TextFieldView alloc] initWithFrame:CGRectMake(w * 2, SafeAreaTopHeight, w, h)];
    [_countField setTitle:titles[2]];
    [_countField setFieldNumber:1];
    _countField.block = ^(NSString * _Nullable content) {
        self->_segmenView.hidden =  content.length > 0 && content.intValue > 1 ? NO : YES;
        self.type = content.length > 0 && content.intValue > 1 ? TypeMany : TypeSingle;
        _count = content.intValue;
    };
    
    CGFloat contentLb_H = 100;
    CGFloat contenLb_Y = self.view.frame.size.height / 2 - contentLb_H / 2;
    _contenlab = [[UILabel alloc] initWithFrame:CGRectMake(0, contenLb_Y, self.view.frame.size.width, contentLb_H)];
    _contenlab.textAlignment = NSTextAlignmentCenter;
    _contenlab.font = [UIFont boldSystemFontOfSize:80];
    _contenlab.textColor = [UIColor whiteColor];
    
    CGFloat tipsLb_H = 100;
    CGFloat tipsLb_Y = self.view.frame.size.height - SafeAreaBottomHeight - tipsLb_H - 20;
    _tipsLb = [[UILabel alloc] initWithFrame:CGRectMake(0, tipsLb_Y, self.view.frame.size.width, tipsLb_H)];
    _tipsLb.numberOfLines = 0;
    _tipsLb.font = [UIFont systemFontOfSize:15];
    _tipsLb.textAlignment = NSTextAlignmentCenter;
    _tipsLb.text = @"点击屏幕\n\n或\n\n摇一摇生成幸运数字";
    _tipsLb.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_minField];
    [self.view addSubview:_maxField];
    [self.view addSubview:_countField];
    [self.view addSubview:_contenlab];
    [self.view addSubview:_tipsLb];
    
    _segmenView = [[SwitchMiddleView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_maxField.frame) + 10, self.view.frame.size.width, 30)];
    _segmenView.hidden = YES;
    _segmenView.block = ^(BOOL status) {
        self->_repeat = status;
    };
    _segmenView.hisblock = ^{
        NSString *content = @"";
        for (int i = 0; i < self.numbers.count; i++) {
          content = [content stringByAppendingFormat:@",%@",self.numbers[i]];
        }
        [self showAlertView:@"历史记录" content:content actionTitle:@"确定"];
    };
    [self.view addSubview:_segmenView];
    
}
- (void)setGraColors {
    int from = 0;
    int to = (int)[self colorsArray].count - 1;
    _graLayer.colors = [self colorsArray][[self getRandomNumber:from to:to]];
}
// 获得随机数
-(int)getRandomNumber:(int)from to:(int)to {
    return ABS((int)arc4random() % (to - from + 1));
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self setGraColors];
    [self caculRandomNumer];
    [self.view endEditing:YES];
}
- (void)showComltion {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                             message:@"已经生成足够数量的幸运数字，\n是否重新生成"
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    // 2.创建并添加按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK Action");
    }];
    UIAlertAction *remakeAction = [UIAlertAction actionWithTitle:@"重新生成" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self.view endEditing:YES];
        [self.numbers removeAllObjects];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:remakeAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)caculRandomNumer {
    if ([_countField getNumber] == 0 ) {
        [self showComltion];
    }else {
        if ([_maxField getNumber] < [_minField getNumber]) {
            [_maxField setFieldNumber:0];
            [_minField setFieldNumber:0];
            [self showAlertView:@"无效的输入" content:@"无效的数字范围" actionTitle:@"重新输入"];
        }else {
           NSString * randomstr = [NSString stringWithFormat:@"%d",[self getRandomNumber:[_minField getNumber] to:[_maxField getNumber]]];
            if (self.type == TypeSingle) {
                _contenlab.text = randomstr;
            }else {
                if (_repeat) {
                    if (self.numbers.count == [_countField getNumber]) {
                        [self showComltion];
                        return ;
                    }
                    _contenlab.text = randomstr;
                    [self.numbers addObject:randomstr];
                }else {
                    if ([_minField getNumber] + [_maxField getNumber] <= [_countField getNumber]) {
                        [self showAlertView:@"无效的输入" content:@"对于不重复数目来会所，这生成的数量\n太多了。" actionTitle:@"重新输入"];
                    }
                }
            }
        }
    }
}
- (void)showAlertView:(NSString *)title
              content:(NSString *)content
          actionTitle:(NSString *)actionTitle
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:content
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    // 2.创建并添加按钮
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.view endEditing:YES];
    }];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (NSMutableArray *)numbers {
    if (!_numbers) {
        _numbers = [NSMutableArray array];
    }
    return _numbers;
}
- (NSArray *)colorsArray {
    return @[@[(id)RGB(65, 128, 180).CGColor,
               (id)RGB(116, 146, 146).CGColor,
               (id)RGB(161, 158, 102).CGColor,
               (id)RGB(233, 197, 73).CGColor],
             @[(id)RGB(224, 120, 124).CGColor,
               (id)RGB(216, 113, 133).CGColor,
               (id)RGB(208, 107, 152).CGColor,
               (id)RGB(199, 101, 152).CGColor],
             @[(id)RGB(52, 78, 56).CGColor,
               (id)RGB(56, 93, 97).CGColor,
               (id)RGB(58, 108, 137).CGColor,
               (id)RGB(62, 122, 169).CGColor],
             @[(id)RGB(48, 62, 77).CGColor,
               (id)RGB(80, 80, 84).CGColor,
               (id)RGB(104, 94, 89).CGColor,
               (id)RGB(131, 110, 95).CGColor],
             @[(id)RGB(213, 90, 69).CGColor,
               (id)RGB(222, 161, 65).CGColor,
               (id)RGB(233, 153, 65).CGColor,
               (id)RGB(245, 195, 68).CGColor],
             @[(id)RGB(87, 78, 162).CGColor,
               (id)RGB(83, 96, 173).CGColor,
               (id)RGB(81, 124, 195).CGColor,
               (id)RGB(80, 146, 210).CGColor],
             @[(id)RGB(63, 97, 114).CGColor,
               (id)RGB(94, 92, 103).CGColor,
               (id)RGB(168, 88, 86).CGColor,
               (id)RGB(205, 88, 75).CGColor],
             @[(id)RGB(191, 195, 198).CGColor,
               (id)RGB(175, 179, 182).CGColor,
               (id)RGB(148, 158, 158).CGColor,
               (id)RGB(135, 143, 145).CGColor],
             @[(id)RGB(242, 172, 66).CGColor,
               (id)RGB(200, 164, 91).CGColor,
               (id)RGB(151, 157, 147).CGColor,
               (id)RGB(96, 153, 198).CGColor],
             @[(id)RGB(228, 118, 122).CGColor,
               (id)RGB(225, 150, 139).CGColor,
               (id)RGB(230, 182, 162).CGColor,
               (id)RGB(235, 210, 179).CGColor],
             @[(id)RGB(115, 100, 189).CGColor,
               (id)RGB(145, 109, 192).CGColor,
               (id)RGB(183, 119, 193).CGColor,
               (id)RGB(221, 130, 191).CGColor],
             @[(id)RGB(90, 185, 158).CGColor,
               (id)RGB(92, 188, 149).CGColor,
               (id)RGB(97, 192, 134).CGColor,
               (id)RGB(102, 198, 127).CGColor],
             @[(id)RGB(236, 230, 239).CGColor,
               (id)RGB(194, 218, 202).CGColor,
               (id)RGB(156, 201, 168).CGColor,
               (id)RGB(100, 177, 116).CGColor],
             @[(id)RGB(90, 71, 55).CGColor,
               (id)RGB(85, 101, 69).CGColor,
               (id)RGB(82, 135, 85).CGColor,
               (id)RGB(83, 160, 97).CGColor],

        ];
}
@end
